﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using com.zhongjh.Library.BaseClass;
using com.zhongjh.Library.Configuration;

namespace ConfiguratorForWinForm
{
    public class MainFormView : ViewBase
    {
        #region 字段

        private MainForm mMainForm = null;
        private string mConfigPath = AppDomain.CurrentDomain.BaseDirectory;
        private string mConfigName = "Configuration.xml";
        private Config mConfig = null;

        #endregion //end 字段

        #region 公有方法

        public MainFormView(FormBase formBase)
            : base(formBase)
        {
            mMainForm = formBase as MainForm;
            mConfig = new Config(mConfigPath + mConfigName);
        }

        //主窗口加载
        public void MainFormLoad()
        {
            List<ConfigItem> configItemList = mConfig.GetConfig();

            TreeNode tnTop = new TreeNode("配置");
            tnTop.ContextMenuStrip = mMainForm.m_configMenu;
            tnTop.Expand();

            foreach (ConfigItem temp in configItemList)
            {
                TreeNode tnItem = new TreeNode(string.Format("[{0}].[{1}]", temp.Key, temp.Value));
                tnItem.ContextMenuStrip = mMainForm.m_configMenu;
                tnItem.Tag = temp;
                AddNode(tnItem, temp);
                tnTop.Nodes.Add(tnItem);
            }

            mMainForm.m_tvConfig.Nodes.Clear();
            mMainForm.m_tvConfig.Nodes.Add(tnTop);
        }

        //选择节点
        public void SelectNode(TreeNode selectNode)
        {
            if (selectNode != null)
            {
                mMainForm.m_gbConfigNode.Tag = selectNode;
                ConfigItem item = selectNode.Tag as ConfigItem;
                if (item != null)
                {
                    mMainForm.m_txtKey.Enabled = true;
                    mMainForm.m_txtValue.Enabled = true;
                    mMainForm.m_txtRemark.Enabled = true;

                    mMainForm.m_txtKey.Text = item.Key;
                    mMainForm.m_txtValue.Text = item.Value;
                    mMainForm.m_txtRemark.Text = item.Remark;
                }
                else
                {
                    mMainForm.m_txtKey.Enabled = false;
                    mMainForm.m_txtValue.Enabled = false;
                    mMainForm.m_txtRemark.Enabled = false;

                    mMainForm.m_txtKey.Text = string.Empty;
                    mMainForm.m_txtValue.Text = string.Empty;
                    mMainForm.m_txtRemark.Text = string.Empty;
                }
            }
        }

        //添加新节点
        public void AddNewNode()
        {
            TreeNode selectNode = mMainForm.m_gbConfigNode.Tag as TreeNode;
            if (selectNode != null)
            {
                ConfigItem selectItem = selectNode.Tag as ConfigItem;

                ConfigItem configItem = new ConfigItem();
                configItem.Key = "NewKey";
                configItem.Value = "NewValue";
                if (selectItem != null)
                    selectItem.ChildItems.Add(configItem);

                if (selectNode.Text == "配置")
                {
                    mConfig.GetConfig().Add(configItem);
                }

                TreeNode tr = new TreeNode(string.Format("[{0}].[{1}]", configItem.Key, configItem.Value));
                tr.ContextMenuStrip = mMainForm.m_configMenu;
                tr.Tag = configItem;
                selectNode.Nodes.Add(tr);
                mMainForm.m_tvConfig.SelectedNode = tr;
            }
        }

        public void DeleteNode()
        {
            TreeNode selectNode = mMainForm.m_gbConfigNode.Tag as TreeNode;
            if (selectNode != null)
            {
                ConfigItem selectItem = selectNode.Tag as ConfigItem;

                if (selectNode.Text == "配置")
                {
                    MessageBox.Show("配置节点不能删除。", "提示");
                    return;
                }
                else if (selectNode.Parent.Text == "配置")
                {
                    mConfig.GetConfig().Remove(selectItem);
                }
                else if (selectNode.Parent != null)
                {
                    TreeNode parentNode = selectNode.Parent;
                    ConfigItem parentItem = parentNode.Tag as ConfigItem;
                    parentItem.ChildItems.Remove(selectItem);
                }
                selectNode.Remove();


            }
        }

        //保存配置
        public void SaveConfig()
        {
            mConfig.Save();
            MessageBox.Show("保存成功。", "提示");
        }

        //加载配置
        public void LoadConfig()
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.InitialDirectory = mConfigPath;
            openDialog.ShowDialog();
            if (!string.IsNullOrEmpty(openDialog.FileName))
            {
                mConfig = new Config(openDialog.FileName);
                MainFormLoad();
            }
        }

        #endregion //end 公有方法

        #region 私有方法

        //添加节点
        private void AddNode(TreeNode parentNode, ConfigItem parentItem)
        {
            foreach (ConfigItem temp in parentItem.ChildItems)
            {
                TreeNode tnItem = new TreeNode(string.Format("[{0}].[{1}]", temp.Key, temp.Value));
                tnItem.ContextMenuStrip = mMainForm.m_configMenu;
                tnItem.Tag = temp;
                AddNode(tnItem, temp);
                parentNode.Nodes.Add(tnItem);
            }
        }

        #endregion //end 私有方法
    }
}
